package com.shinhye.wisdompeking.pager;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.shinhye.wisdompeking.R;
import com.shinhye.wisdompeking.base.BasePager;
import com.shinhye.wisdompeking.bean.NewsCenter.Children;
import com.shinhye.wisdompeking.bean.NewsCenter.NewCenterItem;
import com.shinhye.wisdompeking.custom.pagerindicator.TabPageIndicator;

public class NewsPager extends BasePager {
	private static final String TAG = "NewsPager";
	private NewCenterItem newsItem;
	private TabPageIndicator tpi_indicator;
	private ImageView iv_edit_cate;
	private ViewPager pager;
	private List<Children> childrens;
	private ArrayList<BasePager> items;
	private NewsItemAdapter adapter;
	
	public NewsPager(Context context, NewCenterItem newsItem) {
		super(context);
		this.newsItem = newsItem;
	}

	@Override
	public View initView() {
		view = View.inflate(context, R.layout.frag_news, null);
		tpi_indicator = (TabPageIndicator) view.findViewById(R.id.indicator);
		iv_edit_cate = (ImageView) view.findViewById(R.id.iv_edit_cate);
		pager = (ViewPager) view.findViewById(R.id.pager);
		
		return view;
	}

	@Override
	public void initData() {
		childrens = newsItem.children;
		items = new ArrayList<BasePager>();
		for(Children children : childrens) {
			items.add(new NewsItemPager(context, children.url));
		}
		//给indicator设置监听器即可同时改变ViewPager
		tpi_indicator.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				if(position == 0){
					Log.i(TAG, "可以划出菜单");
					getMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
				}else{
					Log.i(TAG, "不能划出菜单");
					getMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
				}
				items.get(position).initData();
			}
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {}
			@Override
			public void onPageScrollStateChanged(int state) {}
		});
		
		iv_edit_cate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new Handler().post(new Runnable() {
					@Override
					public void run() {
						tpi_indicator.fullScroll(View.FOCUS_RIGHT);
					}
				});
			}
		});
		
		if(adapter == null) {
			adapter = new NewsItemAdapter();
			pager.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		tpi_indicator.setViewPager(pager, 0);
		
		items.get(0).initData();
	}
	
	class NewsItemAdapter extends PagerAdapter {
		@Override
		public CharSequence getPageTitle(int position) {
			return childrens.get(position).title;
		}
		
		@Override
		public int getCount() {
			return childrens.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View item = items.get(position).getRootView();
			container.addView(item);
			return item;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}
}
